<template>
     <div class="course-list">
          <h2>课程列表</h2>
          <div class="stats">
               <p>总课程数: {{ totalCourses }}</p>
               <p>总学分: {{ totalCredits }}</p>
              </div>
         
          <table>
               <thead>
                    <tr>
                         <th>ID</th>
                         <th>课程名称</th>
                         <th>授课教师</th>
                         <th>学分</th>
                         <th>操作</th>
                        </tr>
                   </thead>
               <tbody>
                    <tr v-for="course in courses" :key="course.id">
                         <td>{{ course.id }}</td>
                         <td>{{ course.name }}</td>
                         <td>{{ course.teacher }}</td>
                         <td>{{ course.credit }}</td>
                         <td>
                              <button @click="editCourse(course)">编辑</button>
                              <button @click="deleteCourse(course.id)">删除</button>
                             </td>
                        </tr>
                   </tbody>
              </table>
         </div>
</template>

<script setup>
import { storeToRefs } from 'pinia';
import { useCourseStore } from '../stores/courseStore';

// 使用 store
const courseStore = useCourseStore();
const { courses, totalCourses, totalCredits } = storeToRefs(courseStore);
const { deleteCourse: deleteCourseInStore } = courseStore;

// 定义emits
const emit = defineEmits(['edit']);

// 编辑课程
const editCourse = (course) => {
    emit('edit', course);
};

// 删除课程
const deleteCourse = (id) => {
    if (confirm('确定要删除这门课程吗?')) {
        deleteCourseInStore(id);
    }
};
</script>

<style scoped>
.course-list {
    margin-top: 2rem;
}

.stats {
    display: flex;
    gap: 1rem;
    margin-bottom: 1rem;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th,
td {
    padding: 0.75rem;
    text-align: left;
    border-bottom: 1px solid #ddd;
}

tr:hover {
    background-color: #f9f9f9;
}

button {
    margin-right: 0.5rem;
    padding: 0.25rem 0.5rem;
    background-color: #f44336;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

button:first-child {
    background-color: #2196f3;
}

button:hover {
    opacity: 0.8;
}
</style>